<?php
class data_admin_article extends data_article {
	public function __construct() {
		parent::__construct();
		
	}
	
	public function ls ($is_checked_by_admin=0, $special_flag=0, $page=1, $limit=20) {
		$is_checked_by_admin	= intval($is_checked_by_admin);
		$special_flag		= intval($special_flag);
		$page 			= ($page>1) 	? intval($page) 	: 1;
		$limit				= ($limit>1) 	? intval($limit) 	: 1;
		
		$execute = $this->db->execute('
			select
				save_article.*,
				users.nickname				AS user_nickname,
				IF((select id from recommend_article where article_id=save_article.id limit 1), 1, 0) 	AS is_recommend,
				IF((select id from home_hot where article_id=save_article.id limit 1), 1, 0) 		AS is_hot,
				IF((select id from circle_article where article_id=save_article.id limit 1), 1, 0) 		AS is_circle
			from
				save_article, users
			where
				save_article.is_checked_by_admin	= "'.$is_checked_by_admin.'"			and
				save_article.special_flag			= "'.$special_flag.'"				and
				users.id					= save_article.user_id
			order by
				save_article.id DESC
			limit
				'.(($page-1)*$limit).', '.$limit.'
		');
		return $execute;
	}
	
	public function ls_hot ($page=1, $limit=20) {
		$page 	= ($page>1)  ? intval($page)  : 1;
		$limit	= ($limit>1) ? intval($limit) : 1;
		
		$execute = $this->db->execute('
			select
				*
			from
				home_hot
			where
				1
			order by
				id DESC
			limit
				'.(($page-1)*$limit).', '.$limit.'
		');
		return $execute;
	}
	
	public function one ($id) {
		$id		= intval($id);
		
		if ($id) {
			$execute = $this->db->execute('
				select
					save_article.*,
					article_url.url
				from
					save_article, article_url
				where
					save_article.id		= "'.$id.'"				and
					article_url.id		= save_article.url_id
			');
			return $execute[0];
		} else return false;
	}
	
	public function set_to_pass ($id) {
		$id = intval($id);
		if ($id) {
			return $this->db->execute('
				update
					save_article
				set
					is_checked_by_admin = 1
				where
					id	= "'.$id.'"
			');
		} else return false;
	}
	/**
	 * 设定hot
	 * 		首先先设为PASS
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	public function set_to_hot ($id) {
		$id = intval($id);
		if ($id) {
			$this->set_to_pass($id);		// 设为pass
				$article = $this->db->execute('
					select
						save_article.id			AS id,
						save_article.user_id		AS user_id,
						save_article.url_id			AS url_id,
						save_article.title			AS title,
						save_article.summary		AS summary,
						save_article.img_0			AS img_0,
						save_article.img_1			AS img_1,
						save_article.img_2			AS img_2,
						article_url.url			AS url,
						users.nickname			AS user_nickname
					from
						save_article, article_url, users
					where
						save_article.id 		= "'.$id.'"			and
						article_url.id		= save_article.url_id	and
						users.id			= save_article.user_id
				');
				if ($article) {
					$article = $article[0];
					// 检查是否存在于圈子中
					$circle = $this->db->execute('
						select
							circle.id,
							circle.title
						from
							circle_article, circle
						where
							circle_article.article_id	= "'.$article['id'].'"	and
							circle.id			= circle_article.circle_id	and
							circle.privacy		= "public"
						limit
							1
					');
					if ($circle) {
						$circle = $circle[0];
					} else {
						$circle['id'] 		= 0;
						$circle['title'] 	= '';
					}
					// 检查是否被推荐过
					$recommend = $this->db->execute('
						select
							recommend_article.receiver_id	AS receiver_id,
							users.nickname				AS receiver_nickname
						from
							recommend_article, users
						where
							recommend_article.article_id	= "'.$article['id'].'"				and
							users.id				= recommend_article.receiver_id
						limit
							1
					');
					if ($recommend) {
						$recommend = $recommend[0];
					} else {
						$recommend['receiver_id'] 		= 0;
						$recommend['receiver_nickname'] = '';
					}
					// 写入 home_hot表
					$copy = $this->db->execute('
						insert into
							home_hot
						set
							article_id		= "'.$article['id'].'",
							article_title		= "'.$article['title'].'",
							url_id			= "'.$article['url_id'].'",
							url			= "'.$article['url'].'",
							user_id		= "'.$article['user_id'].'",
							user_nickname	= "'.addslashes($article['user_nickname']).'",
							circle_id		= "'.$circle['id'].'",
							circle_title		= "'.addslashes($circle['title']).'",
							receiver_id		= "'.$recommend['receiver_id'].'",
							receiver_nickname	= "'.addslashes($recommend['receiver_nickname']).'",
							summary			= "'.addslashes($article['summary']).'",
							img_0			= "'.addslashes($article['img_0']).'",
							img_1			= "'.addslashes($article['img_1']).'",
							img_2			= "'.addslashes($article['img_2']).'"
					');
					return $copy;
				} else return false;
		} else return false;
	}
	/**
	 * 删除home hot表中的记录
	 * 
	 *
	 * @param int $id		// 这个是article id
	 */
	public function set_un_hot ($article_id) {
		$article_id = intval($article_id);
		
		if ($article_id) {
			return $this->db->execute('
				delete from
					home_hot
				where
					article_id = "'.$article_id.'"
			');
		} else return false;
	}
	
	public function ls_circle ($page=1, $limit=20) {
		$page 	= ($page>1) ? $page : 1;
		$limit		= ($limit>1) ? $limit : 1;
		
		$execute = $this->db->execute('
			select
				*,
				IF((select id from home_hot_circle where circle_id=circle.id), 1, 0) AS is_hot
			from
				circle
			where
				id NOT IN (select circle_id from home_hot_circle)
			order by
				total_article DESC, 
				total_member DESC,
				total_follower DESC
			limit
				'.(($page-1)*$limit).', '.$limit.'
		');
		return $this->format_circle_article($execute);
	}
	public function ls_circle_hot ($page=1, $limit=20) {
		$page 	= ($page>1) ? $page : 1;
		$limit		= ($limit>1) ? $limit : 1;
		
		$execute = $this->db->execute('
			select
				circle.*
			from
				circle, home_hot_circle
			where
				circle.id = home_hot_circle.circle_id
			order by
				home_hot_circle.id DESC
			limit
				'.(($page-1)*$limit).', '.$limit.'
		');
		return $this->format_circle_article($execute);
	}
	private function format_circle_article ($execute) {
		if (is_array($execute)) {
			foreach ($execute as $k=>$v) {
				$execute[$k]['articles'] = $this->db->execute('
					select
						save_article.id,
						save_article.title,
						save_article.dt
					from
						circle_article, save_article
					where
						circle_article.circle_id	= "'.$v['id'].'"			and
						save_article.id		= circle_article.article_id
					order by
						circle_article.id DESC
					limit
						5
				');
			}
			return $execute;
		} else return false;
	}
	public function set_circle_hot ($circle_id) {
		$circle_id = intval($circle_id);
		if ($circle_id) {
			return $this->db->execute('
				insert ignore into
					home_hot_circle
				set
					circle_id	= "'.$circle_id.'"
			');
		} else return false;
	}
	public function set_circle_unhot ($circle_id) {
		$circle_id = intval($circle_id);
		if ($circle_id) {
			return $this->db->execute('
				delete from
					home_hot_circle
				where
					circle_id = "'.$circle_id.'"
			');
		} else return false;
	}
	public function ls_error_dev () {
		return $this->db->execute('
			select
				*
			from
				error_devel
			order by
				id DESC
		');
	}
	public function del_error_dev ($id) {
		$id = intval($id);
		if ($id) {
			return $this->db->execute('
				delete from
					error_devel
				where
					id = "'.$id.'"
			');
		} else return false;
	}
}
?>